package com.highcom;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;

@SpringBootTest
@DisplayName("断言测试")
public class Springboot03Junit5_1 {


    @Test
    @DisplayName("测试简单断言")
    public void testsimpleAssertions(){
        int cal = cal(3,2);
        assertEquals(6,cal,"业务逻辑计算失败");
    }
    @Test
    @DisplayName("测试简单断言")
    public void testsimpleAssertions2(){
        Object obj1 = new Object();
        Object obj2 = new Object();
        assertSame(obj1,obj2,"两个对象不一样");
    }

    @Test
    @DisplayName("测试数组断言")
    public void testArray(){
        assertArrayEquals(new int[]{1,2},new int[]{1,2},"数组内容不相等");
    }


    @Test
    @DisplayName("测试组合断言")
    public void all(){
        assertAll("test",
                ()->assertTrue(true && true,"结果不为true"),
                ()->assertEquals(1,1,"结果不是1")
        );
    }

    @Test
    @DisplayName("测试异常断言")
    public void testException(){
        assertThrows(ArithmeticException.class,()->{
            int i = 2/0;
        },"业务竟然照常运行了！");
    }


    int cal(int i,int j){
        return i + j;
    }

    @Test
    @DisplayName("超时测试")
    public void timeoutTest() {
        //如果测试方法时间超过1s将会异常
        assertTimeout(Duration.ofMillis(1000), () -> Thread.sleep(1500));
    }

    @Test
    @DisplayName("fail")
    public void shouldFail() {
        fail("This should fail");
    }

}
